package controller

import (
	"strconv"

	"github.com/gogf/gf/v2/net/ghttp"

	"goldtree/internal/service"
	"goldtree/utility/response"
)

// TagController 标签控制器
type TagController struct{}

var TagCtrl = TagController{}

// Create 创建标签
func (c *TagController) Create(r *ghttp.Request) {
	var req *service.CreateTagReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	result, err := service.Tag.Create(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// Update 更新标签
func (c *TagController) Update(r *ghttp.Request) {
	var req *service.UpdateTagReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	err := service.Tag.Update(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r)
}

// Delete 删除标签
func (c *TagController) Delete(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "标签ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "标签ID格式错误")
		return
	}

	err = service.Tag.Delete(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r)
}

// GetById 根据ID获取标签
func (c *TagController) GetById(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "标签ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "标签ID格式错误")
		return
	}

	result, err := service.Tag.GetById(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	if result == nil {
		response.ErrorExit(r, "标签不存在")
		return
	}

	response.SuccessExit(r, result)
}

// GetDetail 获取标签详情
func (c *TagController) GetDetail(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "标签ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "标签ID格式错误")
		return
	}

	result, err := service.Tag.GetDetail(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// GetList 获取标签列表
func (c *TagController) GetList(r *ghttp.Request) {
	var req *service.GetTagListReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	result, err := service.Tag.GetList(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.PageExit(r, result.List, result.Total, result.Page, result.Limit)
}

// GetActiveList 获取激活的标签列表
func (c *TagController) GetActiveList(r *ghttp.Request) {
	result, err := service.Tag.GetActiveTags(r.Context())
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// GetByCategory 根据分类获取标签列表
func (c *TagController) GetByCategory(r *ghttp.Request) {
	category := r.Get("category").String()
	if category == "" {
		response.ErrorExit(r, "标签分类不能为空")
		return
	}

	result, err := service.Tag.GetByCategory(r.Context(), category)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// GetCategoryList 获取标签分类列表
func (c *TagController) GetCategoryList(r *ghttp.Request) {
	result, err := service.Tag.GetCategoryList(r.Context())
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// GetTagTree 获取标签树结构
func (c *TagController) GetTagTree(r *ghttp.Request) {
	result, err := service.Tag.GetTagTree(r.Context())
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// AssignTag 分配标签给股票
func (c *TagController) AssignTag(r *ghttp.Request) {
	var req *service.AssignTagReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	err := service.Tag.AssignTag(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, "分配标签成功")
}

// BatchAssignTag 批量分配标签给股票
func (c *TagController) BatchAssignTag(r *ghttp.Request) {
	var req *service.BatchAssignTagReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	err := service.Tag.BatchAssignTag(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, "批量分配标签成功")
}

// RemoveTag 移除股票标签
func (c *TagController) RemoveTag(r *ghttp.Request) {
	stockCode := r.Get("stock_code").String()
	tagIdStr := r.Get("tag_id").String()

	if stockCode == "" {
		response.ErrorExit(r, "股票代码不能为空")
		return
	}

	if tagIdStr == "" {
		response.ErrorExit(r, "标签ID不能为空")
		return
	}

	tagId, err := strconv.Atoi(tagIdStr)
	if err != nil {
		response.ErrorExit(r, "标签ID格式错误")
		return
	}

	err = service.Tag.RemoveTag(r.Context(), stockCode, tagId)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, "移除股票标签成功")
}

// GetStockTags 获取股票的标签列表
func (c *TagController) GetStockTags(r *ghttp.Request) {
	stockCode := r.Get("stock_code").String()
	if stockCode == "" {
		response.ErrorExit(r, "股票代码不能为空")
		return
	}

	result, err := service.Tag.GetStockTags(r.Context(), stockCode)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// GetTagStocks 获取标签的股票列表
func (c *TagController) GetTagStocks(r *ghttp.Request) {
	tagIdStr := r.Get("tag_id").String()
	if tagIdStr == "" {
		response.ErrorExit(r, "标签ID不能为空")
		return
	}

	tagId, err := strconv.Atoi(tagIdStr)
	if err != nil {
		response.ErrorExit(r, "标签ID格式错误")
		return
	}

	pageStr := r.Get("page").String()
	page := 1
	if pageStr != "" {
		page, err = strconv.Atoi(pageStr)
		if err != nil || page < 1 {
			page = 1
		}
	}

	limitStr := r.Get("limit").String()
	limit := 20
	if limitStr != "" {
		limit, err = strconv.Atoi(limitStr)
		if err != nil || limit < 1 || limit > 100 {
			limit = 20
		}
	}

	stocks, total, err := service.Tag.GetTagStocks(r.Context(), tagId, page, limit)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.PageExit(r, stocks, total, page, limit)
}